package com.dailyblue.project.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.Complaint;
import com.dailyblue.project.estate.commons.handler.ComplaintException;
import com.dailyblue.project.estate.commons.mapper.ComplaintMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.service.ComplaintService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:Liu
 * @Testname:ComplaintServiceImpl
 * @Date:2025/9/18 15:14
 */
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintService {
    @Resource
    private ComplaintMapper complaintMapper;
    @Override
    public JsonResult SaveComplaint(Complaint complaint) {
        boolean b = save(complaint);
        if (!b) {
            throw new ComplaintException("添加失败");
        }
        return ResultTool.success("save complaint");
    }

    @Override
    public JsonResult findOne(String id) {
        Complaint complaint = getById(id);
        return ResultTool.success(complaint);
    }

    @Cacheable(cacheNames = "AllComplaint")
    public JsonResult findAllComplaint() {
        List<Complaint> list = list();
        return ResultTool.success(list);
    }

    @CacheEvict(cacheNames = "AllComplaint",allEntries = true)
    public JsonResult deleteComplaint(String id) {
        boolean b = removeById(id);
        if (!b) {
            throw new ComplaintException("删除失败");
        }
        return ResultTool.success("delete complaint");
    }

    @CacheEvict(cacheNames = "AllComplaint",allEntries = true)
    public JsonResult updateComplaint(Complaint complaint) {
        UpdateWrapper<Complaint> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", complaint.getId());
        wrapper.set("community_id", complaint.getCommunityId());
        wrapper.set("community_name", complaint.getCommunityName());
        wrapper.set("owner_id", complaint.getOwnerId());
        wrapper.set("owner_name", complaint.getOwnerName());
        wrapper.set("description", complaint.getDescription());
        wrapper.set("status", complaint.getStatus());
        wrapper.set("reason", complaint.getReason());
        int update = complaintMapper.update(wrapper);
        if (update != 1) {
            throw new ComplaintException("更新失败");
        }
        return ResultTool.success("update complaint");
    }
}
